﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using XRM.Security.DataAccess.Enums;
using XRM.Security.WebUI.Helpers;
using XRM.Security.WebUI.Models.Department;
using XRM.Security.WebUI.Models.InfoSystem;
using XRM.Security.WebUI.Models.SkziKind;
using XRM.Security.WebUI.Models.UserRegister;
using XRM.Security.WebUI.Models.WorkKind;
using db = XRM.Security.DataAccess.Models;

namespace XRM.Security.WebUI.Models.Request
{
    public class RequestWorkKindModel : BaseViewModel
    {
        public RequestWorkKindModel()
        {
            WorkKindParamList = new List<RequestWorkKindParamModel>();
        }

        public RequestWorkKindModel(db.RequestWorkKind dbEntity)
        {
            WorkKindParamList = new List<RequestWorkKindParamModel>();
            if (dbEntity != null)
            {
                Id = dbEntity.RequestWorkKindId;
                RequestId = dbEntity.RequestId;
                WorkKindId = dbEntity.WorkKindId;
                WorkKind = new WorkKindModel(dbEntity.WorkKind, null);

                if (dbEntity.RequestWorkKindParams != null && dbEntity.RequestWorkKindParams.Any())
                {
                    foreach (db.RequestWorkKindParam item in dbEntity.RequestWorkKindParams)
                    {
                        WorkKindParamList.Add(new RequestWorkKindParamModel(item));
                    }
                }
                CloseDate = dbEntity.CloseDate;
                if (CloseDate.HasValue)
                    ReadOnly = true;
                else if (dbEntity.Request.Status == (int)RequestStatus.Error)
                    ReadOnly = true;
            }
        }

        public RequestWorkKindModel(db.WorkKind dbEntity)
        {
            WorkKindParamList = new List<RequestWorkKindParamModel>();
            if (dbEntity != null)
            {
                Id = -1;
                RequestId = -1;
                WorkKindId = dbEntity.WorkKindId;
                WorkKind = new WorkKindModel(dbEntity, null);

                if (dbEntity.Spec != null && dbEntity.Spec.Any())
                {
                    foreach (db.WorkKindSpec item in dbEntity.Spec)
                    {
                        WorkKindParamList.Add(new RequestWorkKindParamModel(item));
                    }
                }
            }
        }

        public void GetWorkKind(db.WorkKind workKind)
        {
            WorkKind = new WorkKindModel(workKind, null);
        }

        public void GetDepartmentAll(List<db.Department> listAll)
        {
            DepartmentAllList = new List<DepartmentModel>();
            DepartmentNotDeletedList = new List<DepartmentModel>();

            if (listAll != null && listAll.Any())
            {
                foreach (db.Department item in listAll)
                {
                    DepartmentAllList.Add(new DepartmentModel(item));
                    if (!item.IsDeleted)
                        DepartmentNotDeletedList.Add(new DepartmentModel(item));
                }
               
            }
            DepartmentAllList.Insert(0, new DepartmentModel() { Id = -1, Name = "(Выберите подразделение)" });
            DepartmentNotDeletedList.Insert(0, new DepartmentModel() { Id = -1, Name = "(Выберите подразделение)" });
        }

        public void GetSkziKindAll(List<db.SkziKind> listAll)
        {
            SkziKindAllList = new List<SkziKindModel>();
            SkziKindNotDeletedList = new List<SkziKindModel>();

            if (listAll != null && listAll.Any())
            {
                foreach (db.SkziKind item in listAll)
                {
                    SkziKindAllList.Add(new SkziKindModel(item));
                    if (!item.IsDeleted)
                        SkziKindNotDeletedList.Add(new SkziKindModel(item));
                }
            }

            SkziKindAllList.Insert(0, new SkziKindModel() { Id = -1, ShortName = "(Выберите вид СКЗИ)" });
            SkziKindNotDeletedList.Insert(0, new SkziKindModel() { Id = -1, ShortName = "(Выберите вид СКЗИ)" });
        }

        public List<ListItem> StatusDocList
        {
            get
            {
                List<ListItem> result = new List<ListItem>();
                foreach (DocStatus item in (DocStatus[])Enum.GetValues(typeof(DocStatus)))
                {
                    result.Add(new ListItem { Text = item.Description(), Value = ((int)item).ToString() });
                }
                return result;
            }
        }

        public void GetPCUser(List<db.PC> listAll)
        {
            PCUserList = new List<PCModel>();

            if (listAll != null && listAll.Any())
            {
                foreach (var item in listAll)
                {
                    PCUserList.Add(new PCModel(item));
                }

                PCUserList.Insert(0, new PCModel() { Id = -1, SerialNumber = "(Выберите серийный номер)" });
            }
        }

        public void GetInfoSystemUser(List<db.UserInfoSystem> listAll)
        {
            InfoSystemUserList = new List<InfoSystemModel>();

            if (listAll != null && listAll.Any())
            {
                foreach (db.UserInfoSystem item in listAll)
                {
                    InfoSystemUserList.Add(new InfoSystemModel(item.InfoSystem));
                }
            }
            InfoSystemUserList.Insert(0, new InfoSystemModel() { Id = -1, InfoSystemName = "(Выберите информационную систему)", InMaintenance = true });
        }

        public void GetInfoSystemUser(List<db.InfoSystem> listAll)
        {
            InfoSystemUserList = new List<InfoSystemModel>();

            if (listAll != null && listAll.Any())
            {
                foreach (db.InfoSystem item in listAll)
                {
                    InfoSystemUserList.Add(new InfoSystemModel(item));
                }
            }
            InfoSystemUserList.Insert(0, new InfoSystemModel() { Id = -1, InfoSystemName = "(Выберите информационную систему)", InMaintenance = true });
        }

        public int RequestId { get; set; }
        public int WorkKindId { get; set; }
        public int WorkKindIndex { get; set; }
        public string WorkKindContId { get; set; }
        [Display(Name = "Дата закрытия")]
        public DateTime? CloseDate { get; set; }
        public int UserRegisterId { get; set; }
        public WorkKindModel WorkKind { get; set; }
        public List<RequestWorkKindParamModel> WorkKindParamList { get; set; }

        public List<DepartmentModel> DepartmentAllList { get; set; }
        public List<DepartmentModel> DepartmentNotDeletedList { get; set; }
        public List<SkziKindModel> SkziKindAllList { get; set; }
        public List<SkziKindModel> SkziKindNotDeletedList { get; set; }
        
        public List<PCModel> PCUserList { get; set; }
        public List<InfoSystemModel> InfoSystemUserList { get; set; }

    }
}